package com.example.zhunongzonghe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhunongzonghe.beans.ZnExchangePostBean;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.entity.ZnExchangeComment;
import com.example.zhunongzonghe.entity.ZnExchangePost;
import com.example.zhunongzonghe.entity.ZnUsers;
import com.example.zhunongzonghe.service.IZnExchangeCommentService;
import com.example.zhunongzonghe.service.IZnExchangePostService;
import com.example.zhunongzonghe.service.IZnUsersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/znExchangePost")
public class ZnExchangePostController  {
    @Autowired
    private IZnExchangePostService znExchangePostService;
    @Autowired
    private IZnUsersService iZnUsersService;
    @Resource
    private IZnExchangeCommentService commentService;

    /**
     * 显示全部帖子
     * @param currentPage
     * @param pageNum
     * @return
     */
    @GetMapping("/SearchPostsAll")
    public Result SearchPostsAll(@RequestParam("currentPage") Integer currentPage,
                                       @RequestParam("pageSize") Integer pageNum){
        if (currentPage==null && pageNum==null) return new Result(false,"数据异常",400,null);
        Page<ZnExchangePostBean> znExchangePost = znExchangePostService.searchPostAll(currentPage,pageNum);
        if (currentPage > znExchangePost.getPages()){
            int lastPage = (int) znExchangePost.getPages();
            znExchangePost=znExchangePostService.searchPostAll(lastPage,pageNum);
        }
        return new Result<>(true,"查询成功",200,znExchangePost );
    }

    @GetMapping("/postsAll")
    public Result postsAll(@RequestParam("currentPage") Integer currentPage,
                                 @RequestParam("pageSize") Integer pageNum){
        if (currentPage==null && pageNum==null) return new Result(false,"数据异常",400,null);
        Page<ZnExchangePostBean> znExchangePost = znExchangePostService.postAll(currentPage,pageNum);
        if (currentPage > znExchangePost.getPages()){
            int lastPage = (int) znExchangePost.getPages();
            znExchangePost=znExchangePostService.postAll(lastPage,pageNum);
        }
        return new Result<>(true,"查询成功",200,znExchangePost );
    }

    /**
     * 用户登录后查看全部帖子信息，以及是否被自己点赞
     * @param currentPage
     * @param pageNum
     * @param principal
     * @return
     */
    @GetMapping("/LogUserPostAll")
    public Result SearchPostsAll(@RequestParam("currentPage") Integer currentPage,
                                 @RequestParam("pageSize") Integer pageNum,Principal principal){
        if (currentPage==null && pageNum==null) return new Result(false,"数据异常",400,null);
        Page<ZnExchangePostBean> znExchangePost = znExchangePostService.LogUserPostAll(currentPage,pageNum,principal.getName());
        if (currentPage > znExchangePost.getPages()){
            int lastPage = (int) znExchangePost.getPages();
            znExchangePost=znExchangePostService.LogUserPostAll(lastPage,pageNum,principal.getName());
        }
        return new Result<>(true,"查询成功",200,znExchangePost );
    }

    /**
     * 后台管理员搜索帖子
     */
    @GetMapping("/searchPosts")
    public Result searchPosts(@RequestParam("title") Object searchTitle,
                              @RequestParam("currentPage") Integer currentPage,
                              @RequestParam("pageSize") Integer pageNum){
        Page<ZnExchangePostBean> beanPage = znExchangePostService.searchPostsTitle(searchTitle,currentPage,pageNum);
        if (currentPage > beanPage.getPages()){
            beanPage=znExchangePostService.searchPostsTitle(searchTitle, (int) beanPage.getPages(),pageNum);
        }
        return new Result(true,"查询成功",200,beanPage);
    }
    /**
     * 后台管理员审核帖子
     * @param znExchangePost
     * @return
     */
    @PutMapping("/examinePost")
    public Result<ZnExchangePost> examinePost(@RequestBody ZnExchangePost znExchangePost){
        if (znExchangePost==null) return new Result(false,"数据异常",400,null);
        System.out.println("11111111"+znExchangePost);
        znExchangePost.setUpdateTime(LocalDateTime.now());
        znExchangePostService.updateById(znExchangePost);
        return new Result<>(true,"更新成功",200);
    }



    /**
     * 后台管理员删除帖子
     * @param postsId
     * @return
     */
    @DeleteMapping("/deletePost/{postsId}")
    public Result<ZnExchangePost> delete(@PathVariable Integer postsId){
        if (postsId==null) return new Result(false,"数据异常",400,null);
        System.out.println("1111111"+postsId);
        znExchangePostService.removeById(postsId);
        LambdaQueryWrapper<ZnExchangeComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnExchangeComment::getPostId,postsId);
        List<ZnExchangeComment> commentList = commentService.list(queryWrapper);
        commentService.removeByIds(commentList);
        return new Result<>(true,"删除成功",200);
    }
    /**
     * 后台新增帖子
     */
    @PostMapping("/addPost")
    public Result addPost(@RequestBody ZnExchangePost znExchangePost, Principal principal){
        if (znExchangePost==null) return new Result(false,"数据异常",400,null);
        ZnUsers znUsers = iZnUsersService.getUserName(principal.getName());
        znExchangePost.setUserId(znUsers.getId());
        znExchangePost.setStatus(0);
        znExchangePost.setCreateTime(LocalDateTime.now());
        znExchangePostService.save(znExchangePost);
        return new Result(true,"添加成功",200);
    }


    /**
     * 前台跳转帖子详情页
     * @param postId
     * @return
     */
    @GetMapping("/getPostDetail/{postId}")
    public Result getDetailPost(@PathVariable Integer postId){
        if (postId == null){
            return new Result(false,"异常，请联系管理员",400);
        }
        ZnExchangePost znExchangePost = znExchangePostService.getById(postId);
        ZnUsers userNick = iZnUsersService.getById(znExchangePost.getUserId());
        System.out.println("1111111---------"+userNick);
        ZnExchangePostBean znExchangePostBean = new ZnExchangePostBean();
        znExchangePostBean.setNickName(userNick.getNickname());
        znExchangePostBean.setAvatar(userNick.getAvatar());
        BeanUtils.copyProperties(znExchangePost,znExchangePostBean);
        return new Result(true,"成功",200,znExchangePostBean);
    }

    /**
     * 获取当前用户已发布帖子
     * @param currentPage
     * @param pageSize
     * @param principal
     * @return
     */
    @GetMapping("/getMyIssuePost")
    public Result getMyIssuePost(@RequestParam("currentPage") Integer currentPage,
                                 @RequestParam("pageSize") Integer pageSize,Principal principal){
        if (currentPage==null && pageSize==null) return new Result(false,"数据异常",400);
        Page<ZnExchangePostBean> myPost = znExchangePostService.searchMyPost(currentPage,pageSize,principal.getName());
        return new Result(true,"成功",200,myPost);
    }
    /**
     * 用户编辑帖子
     * @param znExchangePost
     * @return
     */
    @PutMapping("/editMyPost")
    public Result<ZnExchangePost> editMyPost(@RequestBody ZnExchangePost znExchangePost){
        if (znExchangePost==null) return new Result(false,"数据异常",400,null);
        System.out.println("11111111"+znExchangePost);
        znExchangePost.setUpdateTime(LocalDateTime.now());
        znExchangePostService.updateById(znExchangePost);
        return new Result<>(true,"成功",200);
    }

    /**
     * 当前用户删除自己已发布的帖子
     * @param postsId
     * @return
     */
    @DeleteMapping("/delMyPost/{postsId}")
    public Result<ZnExchangePost> delMyPost(@PathVariable Integer postsId,Principal principal){
        if (postsId==null) return new Result(false,"数据异常",400,null);
        znExchangePostService.delMyPost(postsId, principal.getName());

        return new Result<>(true,"删除成功",200);
    }
}
